#include <dirent.h>
#include <stdio.h>
#include "sqlite.h"

typedef struct {
    object_t super;

    _Bool is_init;
    _Bool is_restart;
    _Bool is_singleton;
    _Bool is_service;

    int depend_id;
    int chk_cycle;
    int p1_times, p2_times, p3_times;

    _Bool _active;//进程存在
    int _pid;//记录最近一次扫描到的进程pid
    int _counter;//如果进程号变更，计数器+1
    int _fail_counter;//如果进程不存在，计数器+1
    char _file[MAXNAMLEN * 2];//`path/bin`，用于和`/proc/<pid>/exe`快速比较

    char path[MAXNAMLEN];
    char bin[MAXNAMLEN];
    char args[MAXNAMLEN];//非singleton模式，需要和`/proc/<pid>/cmdline`里面的内容比较

    char pid_file[MAXNAMLEN];
    char status_cmd[MAXNAMLEN];
    char start_cmd[MAXNAMLEN];
    char stop_cmd[MAXNAMLEN];

    char p1_cmd[MAXNAMLEN];
    char p2_cmd[MAXNAMLEN];
    char p3_cmd[MAXNAMLEN];
} daemon_t;

daemon_t *daemon_new();

void daemon_free(daemon_t *ptr);

void read_row_daemon(sqlite3_stmt *stmt, daemon_t *rs);

int read_stmt_daemons(sqlite3 *db, vector_t *daemons);

void daemon_find_pid_by_proc(vector_t *daemons);

void daemon_find_pid_by_file(vector_t *daemons);

_Bool daemon_active(daemon_t *daemon);

int daemon_restart(daemon_t *p_daemon);

void daemon_check(daemon_t *p_daemon);